<template>
    <div class="createDevice-container">
        <el-form ref="postForm" :rules="rules" :model="postForm" class="form-container" label-width="150px">

            <sticky :z-index="10" :class-name="'sub-navbar '+postForm.status">
                <VersionGroup v-model="postForm.comment_disabled"/>
                <el-button v-loading="loading" style="margin-left: 10px;" type="success" @click="submitForm">
                    提交保存
                </el-button>
            </sticky>

            <div class="createDevice-main-container">
                <el-form-item label="设备唯一码" required prop="identity">
                    <el-input v-model="postForm.identity" style="width: 80%"></el-input>
                </el-form-item>
                <el-form-item label="设备类型" required prop="type">
                    <el-select v-model="postForm.type" placeholder="请选择设备类型" style="width: 80%">
                        <el-option v-for="type in deviceTypes" :label="type.typeName"
                                   :value="type.typeTag"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="备注信息" prop="desc">
                    <el-input type="textarea" :rows="7" v-model="postForm.desc" style="width: 80%"></el-input>
                </el-form-item>
            </div>
        </el-form>
    </div>
</template>


<script>
    import Tinymce from '@/components/Tinymce'
    import Upload from '@/components/Upload/SingleImage3'
    import Sticky from '@/components/Sticky' // 粘性header组件
    import SystemUser from '@/api/system-user'
    // 设备类型列表
    const deviceTypes = {}
    // 默认的表单数据
    const defaultForm = {
        // 设备唯一码
        identity: '',
        // 设备备注
        desc: '',
        // 设备类型
        type: '',
        status: 'draft',
        comment_disabled: false,
        importance: 0
    }

    export default {
        name: 'CreateDevice',
        components: {Tinymce, Upload, Sticky, fetchTypeList},
        props: {
            isEdit: {
                type: Boolean,
                default: false
            }
        },
        data() {
            return {
                postForm: Object.assign({}, defaultForm),
                deviceTypes: deviceTypes,
                loading: false,
                rules: {
                    identity: [
                        {required: true, message: '请输入设备唯一码'},
                        {min: 1, max: 100, message: '设备唯一码请保持在1~100字符之间'}
                    ],
                    type: [
                        {required: true, message: '请选择设备类型'}
                    ],
                    desc: [
                        {max: 200, message: '备注信息内容请保持在200个字符以内'}
                    ]
                }
            }
        },
        computed: {
            contentShortLength() {
                return this.postForm.content_short.length
            },
            displayTime: {
                // set and get is useful when the data
                // returned by the back end api is different from the front end
                // back end return => "2013-06-25 06:59:25"
                // front end need timestamp => 1372114765000
                get() {
                    return (+new Date(this.postForm.display_time))
                },
                set(val) {
                    this.postForm.display_time = new Date(val)
                }
            }
        },
        created() {
            //this.fillUpDeviceTypeList();
        },
        methods: {
            submitForm() {
                console.log(this.postForm)
                this.$refs.postForm.validate(valid => {
                    if (valid) {
                        this.loading = true
                        createDevice(this.postForm).then(response => {
                            this.postForm.status = 'published'
                            this.loading = false
                            this.$notify({
                                title: '成功',
                                message: '设备添加成功',
                                type: 'success',
                                duration: 2000
                            })
                        }).catch(error => {
                            this.loading = false
                            reject(error)
                        })
                    } else {
                        return false
                    }
                })
            },
            /**
             * 查询设备类型列表
             * 将结果填充到下拉框
             */
            fillUpDeviceTypeList() {
                fetchTypeList().then(response => {
                    this.deviceTypes = response.data;
                }).catch(error => {
                    reject(error)
                });
            }
        }
    }
</script>

<style lang="scss" scoped>
    @import "~@/styles/mixin.scss";

    .createDevice-container {
        position: relative;

        .createDevice-main-container {
            padding: 40px 45px 20px 50px;

            .postInfo-container {
                position: relative;
                @include clearfix;
                margin-bottom: 10px;

                .postInfo-container-item {
                    float: left;
                }
            }
        }
    }
</style>
